var run=true;
let courseName;
let userId_const = localStorage.getItem("userId");
let userName_const = localStorage.getItem("userName");
let username = $(".dropdown-user-name");
username.find('i').remove();
let user_name = '<i>你好，'+userName_const+'老师</i>';
username.append(user_name);                       //生成导航栏的老师名字
username = $("#user_name");
username.find('span').remove();
user_name = '<span>'+userName_const+'老师</span>'      //生成导航栏下拉框的老师名字
username.append(user_name);

let courseId_arr=[];
let courseName_arr=[];
function init(){
    $.ajax({
        url:'/method/getAllJson',
        type:'get',
        dataType:'json',
        async:false,
        data:{"userId":userId_const},
        success:function (data){
            $("#course-target").find('tr').remove();
            courseId_arr=[];
            courseName_arr=[];
            for(let i in data){
                let tr;
                let target;
                let operation;
                courseId_arr.push(data[i].courseId);
                $.ajax({
                    url: '/lv_course/findNameById',
                    type: 'get',
                    dataType: 'json',
                    async:false,
                    data:{"courseId":data[i].courseId},
                    success: function (data) {
                        courseName=data.courseName;
                        courseName_arr.push(courseName);
                        if(data.haveTarget===1){
                            target = '是';
                            operation = '修改';
                        }
                        else {
                            target='否';
                            operation = '录入';
                        }
                    }
                });

                tr='<th scope="row">'+data[i].courseId+'</th>'+'<td>'+courseName+'</td>'+'<td>'+target+'</td>'+'<td><a href="#"> '+operation+'</a></td>'
                $("#course-target").append('<tr>'+tr+'</tr>')
            }
            localStorage.setItem('courseId',courseId_arr);
            localStorage.setItem('courseName',courseName_arr);
            passParam2URL();
        }
    });
}



//将参数通过url传递
function passParam2URL(){
    $("#course-target tr").find("a").click(function(){
        let id = $(this).parents("tr").find('th').text();
        window.open('./target-input.html?courseId='+id,'_self');
    })
}
//给登录按钮绑定回车键
$("body").keydown(function () {
    if (event.keyCode == "13"){    //13是enter键的键码
        $("#btn-find").click();
    }
});
$(document).ready(function(){
    if (run)
        init();
    $("#btn-find").click(function (){
        let inputText = $("#find-course").val();
        let reg = new RegExp("^[0-9]*[1-9][0-9]*$");
        if(inputText.match(/^[ ]*$/)) {
            init();
        }
            if(inputText.match(reg)) {
                let flag=false;
                for(let i = 0;i<courseId_arr.length;i++){
                    if(inputText==courseId_arr[i]){
                        $.ajax({
                            url:'/method/findById',
                            type:'get',
                            dataType: 'json',
                            contentType:'application/json',
                            data:{"courseId":inputText},
                            success:function (data){
                                    $("#course-target").find('tr').remove();
                                    let tr;
                                    let target;
                                    let operation;
                                    $.ajax({
                                        url: '/target/findTargetContentById',
                                        type: 'get',
                                        dataType: 'json',
                                        data: {"courseId": inputText},
                                        success: function () {
                                            $.ajax({
                                                url: '/lv_course/findNameById',
                                                type: 'get',
                                                dataType: 'json',
                                                async:false,
                                                data:{"courseId":data.courseId},
                                                success: function (data) {
                                                    courseName=data.courseName;
                                                    if(data.haveTarget===1){
                                                        target = '是';
                                                        operation = '修改';
                                                    }
                                                    else {
                                                        target='否';
                                                        operation = '录入';
                                                    }
                                                }
                                            });
                                            tr='<th scope="row">'+data.courseId+'</th>'+'<td>'+courseName+'</td>'+'<td>'+target+'</td>'+'<td><a href="#">'+operation+'</a></td>';
                                            $("#course-target").append('<tr>'+tr+'</tr>');
                                            passParam2URL();
                                        }

                                    });
                                }
                        });
                        flag=true;
                        break;
                    }
                }
                if(flag===false)
                    swal({title:"未找到相关课程",type:"warning"});
            }else{
                let flag=false;
                for(let i = 0;i<courseName_arr.length;i++) {
                    if (courseName_arr[i].indexOf(inputText)>=0) {
                        $.ajax({
                            url:'/method/findById',
                            type:'get',
                            dataType: 'json',
                            contentType:'application/json',
                            data:{"courseId":courseId_arr[i]},
                            success:function (data){
                                    $("#course-target").find('tr').remove();
                                    let tr;
                                    let target;
                                    let operation;
                                        $.ajax({
                                            url: '/target/findTargetContentByName',
                                            type: 'get',
                                            dataType: 'json',
                                            data: {"courseName": inputText},
                                            success: function () {
                                                $.ajax({
                                                    url: '/lv_course/findNameById',
                                                    type: 'get',
                                                    dataType: 'json',
                                                    async:false,
                                                    data:{"courseId":data.courseId},
                                                    success: function (data) {
                                                        courseName=data.courseName;
                                                        if(data.haveTarget===1){
                                                            target = '是';
                                                            operation = '修改';
                                                        }
                                                        else {
                                                            target='否';
                                                            operation = '录入';
                                                        }
                                                    }
                                                });
                                                tr='<th scope="row">'+data.courseId+'</th>'+'<td>'+courseName+'</td>'+'<td>'+target+'</td>'+'<td><a href="#">'+operation+'</a></td>';
                                                $("#course-target").append('<tr>'+tr+'</tr>');
                                                passParam2URL();
                                            }

                                        });
                            }
                        });
                        flag=true;
                    }
                }
                if(flag===false)
                    swal({title:"未找到相关课程",type:"warning"});

            }
    })
});